package com.industrial.governance.controller;

import java.util.List;
import java.util.Set;
import javax.servlet.http.HttpServletResponse;

import cn.hutool.core.util.ArrayUtil;
import com.industrial.common.security.utils.SecurityUtils;
import com.industrial.governance.domain.PreMuImportDetailInfo;
import com.industrial.governance.domain.QueryParams;
import com.industrial.governance.service.IPreMuImportDetailInfoService;
import com.industrial.governance.util.PageResult;
import com.industrial.governance.util.PageUtils;
import com.industrial.system.api.domain.SysUser;
import org.apache.commons.lang3.StringUtils;
import org.aspectj.bridge.Message;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.industrial.common.log.annotation.Log;
import com.industrial.common.log.enums.BusinessType;
import com.industrial.common.security.annotation.RequiresPermissions;
import com.industrial.common.core.web.controller.BaseController;
import com.industrial.common.core.web.domain.AjaxResult;
import com.industrial.common.core.utils.poi.ExcelUtil;
import com.industrial.common.core.web.page.TableDataInfo;

/**
 * 导入数据详情Controller
 *
 * @author ruoyi
 * @date 2024-02-29
 */
@RestController
@RequestMapping("/dept/data/cj")
public class PreMuImportDetailInfoController extends BaseController
{
    @Autowired
    private IPreMuImportDetailInfoService preMuImportDetailInfoService;


    @GetMapping("/updateShuiwuJuCount")
    public AjaxResult updateShuiwuJuCount(){

        preMuImportDetailInfoService.selectShuiWuJuData();

        return AjaxResult.success();
    }

    /**
     * 查询统计局导入数据详情列表
     */
    @GetMapping("/list")
    public PageResult<PreMuImportDetailInfo> list(PreMuImportDetailInfo params, @RequestParam("pageNum") String pageNum,@RequestParam("pageSize") String pageSize)
    {
        if(StringUtils.isBlank(params.getYears())){
            throw new RuntimeException("请选择年份");
        }
        Set<String> sysRoles = SecurityUtils.getLoginUser().getRoles();
        SysUser sysUser = SecurityUtils.getLoginUser().getSysUser();

        if(sysUser.isAdmin()){
            sysRoles.add("p_01_011");
        }

        if(ArrayUtil.contains(sysRoles.toArray(),"x_01_011")){
            if(StringUtils.isBlank(params.getCity())){
                params.setCounty(sysUser.getDistrict());
            }else{
                params.setCounty(params.getCity());
                params.setCity(null);
            }
            params.setCity(null);
        }else if(ArrayUtil.contains(sysRoles.toArray(),"s_01_011")){
            if(StringUtils.isBlank(params.getCity())){
                params.setCity(sysUser.getCity());
            }else{
                if(StringUtils.endsWith(params.getCity(),"0000")){
                    params.setProvince(params.getCity());
                    params.setCity(null);
                }else if(StringUtils.endsWith(params.getCity(),"00")){
                    params.setCity(params.getCity());
                }else{
                    params.setCounty(params.getCity());
                    params.setCity(null);
                }
            }
        }else if(ArrayUtil.contains(sysRoles.toArray(),"p_01_011")){
            if(StringUtils.isBlank(params.getCity())){
                params.setProvince(sysUser.getProvince());
            }else{
                if(StringUtils.endsWith(params.getCity(),"0000")){
                    params.setProvince(params.getCity());
                    params.setCity(null);
                }else if(StringUtils.endsWith(params.getCity(),"00")){
                    params.setCity(params.getCity());
                }else{
                    params.setCounty(params.getCity());
                    params.setCity(null);
                }
            }
        }
        params.setDpetCode("tongjiju");
        List<PreMuImportDetailInfo> list = preMuImportDetailInfoService.selectPreMuImportDetailInfoList(params);

        PageResult<PreMuImportDetailInfo> preMuImportDetailInfoPageResult = PageUtils.paginateList(list, Integer.valueOf(pageNum), Integer.valueOf(pageSize));

        return preMuImportDetailInfoPageResult;
    }


    /**
     * 查询税务局导入数据详情列表
     */
    @GetMapping("/shuiwu/list")
    public PageResult<PreMuImportDetailInfo> shuiWuList(PreMuImportDetailInfo params, @RequestParam("pageNum") String pageNum,@RequestParam("pageSize") String pageSize)
    {
        if(StringUtils.isBlank(params.getYears())){
            throw new RuntimeException("请选择年份");
        }

        Set<String> sysRoles = SecurityUtils.getLoginUser().getRoles();
        SysUser sysUser = SecurityUtils.getLoginUser().getSysUser();

        if(sysUser.isAdmin()){
            sysRoles.add("p_01_011");
        }

        if(ArrayUtil.contains(sysRoles.toArray(),"x_01_011")){
            if(StringUtils.isBlank(params.getCity())){
                params.setCounty(sysUser.getDistrict());
            }else{
                params.setCounty(params.getCity());
                params.setCity(null);
            }
        }else if(ArrayUtil.contains(sysRoles.toArray(),"s_01_011")){
            if(StringUtils.isBlank(params.getCity())){
                params.setCity(sysUser.getCity());
            }else{
                if(StringUtils.endsWith(params.getCity(),"0000")){
                    params.setProvince(params.getCity());
                    params.setCity(null);
                }else if(StringUtils.endsWith(params.getCity(),"00")){
                    params.setCity(params.getCity());
                }else{
                    params.setCounty(params.getCity());
                    params.setCity(null);
                }
            }
        }else if(ArrayUtil.contains(sysRoles.toArray(),"p_01_011")){
            if(StringUtils.isBlank(params.getCity())){
                params.setProvince(sysUser.getProvince());
            }else{
                if(StringUtils.endsWith(params.getCity(),"0000")){
                    params.setProvince(params.getCity());
                    params.setCity(null);
                }else if(StringUtils.endsWith(params.getCity(),"00")){
                    params.setCity(params.getCity());
                }else{
                    params.setCounty(params.getCity());
                    params.setCity(null);
                }
            }
        }
        params.setDpetCode("shuiwuju");
        List<PreMuImportDetailInfo> list = preMuImportDetailInfoService.selectPreMuImportDetailInfoList(params);

        PageResult<PreMuImportDetailInfo> preMuImportDetailInfoPageResult = PageUtils.paginateList(list, Integer.valueOf(pageNum), Integer.valueOf(pageSize));

        return preMuImportDetailInfoPageResult;
    }


    /**
     * 查询人社局导入数据详情列表
     */
    @GetMapping("/renshe/list")
    public PageResult<PreMuImportDetailInfo> renSheList(PreMuImportDetailInfo params, @RequestParam("pageNum") String pageNum,@RequestParam("pageSize") String pageSize)
    {

        if(StringUtils.isBlank(params.getYears())){
            throw new RuntimeException("请选择年份");
        }

        Set<String> sysRoles = SecurityUtils.getLoginUser().getRoles();
        SysUser sysUser = SecurityUtils.getLoginUser().getSysUser();

        if(sysUser.isAdmin()){
            sysRoles.add("p_01_011");
        }

        if(ArrayUtil.contains(sysRoles.toArray(),"x_01_011")){
            if(StringUtils.isBlank(params.getCity())){
                params.setCounty(sysUser.getDistrict());
            }else{
                params.setCounty(params.getCity());
                params.setCity(null);
            }
            params.setCity(null);
        }else if(ArrayUtil.contains(sysRoles.toArray(),"s_01_011")){
            if(StringUtils.isBlank(params.getCity())){
                params.setCity(sysUser.getCity());
            }else{
                if(StringUtils.endsWith(params.getCity(),"0000")){
                    params.setProvince(params.getCity());
                    params.setCity(null);
                }else if(StringUtils.endsWith(params.getCity(),"00")){
                    params.setCity(params.getCity());
                }else{
                    params.setCounty(params.getCity());
                    params.setCity(null);
                }
            }
        }else if(ArrayUtil.contains(sysRoles.toArray(),"p_01_011")){
            if(StringUtils.isBlank(params.getCity())){
                params.setProvince(sysUser.getProvince());
            }else{
                if(StringUtils.endsWith(params.getCity(),"0000")){
                    params.setProvince(params.getCity());
                    params.setCity(null);
                }else if(StringUtils.endsWith(params.getCity(),"00")){
                    params.setCity(params.getCity());
                }else{
                    params.setCounty(params.getCity());
                    params.setCity(null);
                }
            }
        }
        params.setDpetCode("rensheju");
        List<PreMuImportDetailInfo> list = preMuImportDetailInfoService.selectPreMuImportDetailInfoList(params);

        PageResult<PreMuImportDetailInfo> preMuImportDetailInfoPageResult = PageUtils.paginateList(list, Integer.valueOf(pageNum), Integer.valueOf(pageSize));

        return preMuImportDetailInfoPageResult;
    }

    /**
     * 查询生态环境局导入数据详情列表
     */
    @GetMapping("/hunabao/list")
    public PageResult<PreMuImportDetailInfo> huanBaoList(PreMuImportDetailInfo params, @RequestParam("pageNum") String pageNum,@RequestParam("pageSize") String pageSize)
    {

        if(StringUtils.isBlank(params.getYears())){
            throw new RuntimeException("请选择年份");
        }

        Set<String> sysRoles = SecurityUtils.getLoginUser().getRoles();
        SysUser sysUser = SecurityUtils.getLoginUser().getSysUser();

        if(sysUser.isAdmin()){
            sysRoles.add("p_01_011");
        }

        if(ArrayUtil.contains(sysRoles.toArray(),"x_01_011")){
            if(StringUtils.isBlank(params.getCity())){
                params.setCounty(sysUser.getDistrict());
            }else{
                params.setCounty(params.getCity());
                params.setCity(null);
            }
            params.setCity(null);
        }else if(ArrayUtil.contains(sysRoles.toArray(),"s_01_011")){
            if(StringUtils.isBlank(params.getCity())){
                params.setCity(sysUser.getCity());
            }else{
                if(StringUtils.endsWith(params.getCity(),"0000")){
                    params.setProvince(params.getCity());
                    params.setCity(null);
                }else if(StringUtils.endsWith(params.getCity(),"00")){
                    params.setCity(params.getCity());
                }else{
                    params.setCounty(params.getCity());
                    params.setCity(null);
                }
            }
        }else if(ArrayUtil.contains(sysRoles.toArray(),"p_01_011")){
            if(StringUtils.isBlank(params.getCity())){
                params.setProvince(sysUser.getProvince());
            }else{
                if(StringUtils.endsWith(params.getCity(),"0000")){
                    params.setProvince(params.getCity());
                    params.setCity(null);
                }else if(StringUtils.endsWith(params.getCity(),"00")){
                    params.setCity(params.getCity());
                }else{
                    params.setCounty(params.getCity());
                    params.setCity(null);
                }
            }
        }
        params.setDpetCode("huanbaoju");
        List<PreMuImportDetailInfo> list = preMuImportDetailInfoService.selectPreMuImportDetailInfoList(params);

        PageResult<PreMuImportDetailInfo> preMuImportDetailInfoPageResult = PageUtils.paginateList(list, Integer.valueOf(pageNum), Integer.valueOf(pageSize));

        return preMuImportDetailInfoPageResult;
    }

    /**
     * 查询生态环境局导入数据详情列表
     */
    @GetMapping("/shuili/list")
    public PageResult<PreMuImportDetailInfo> shuiLiList(PreMuImportDetailInfo params, @RequestParam("pageNum") String pageNum,@RequestParam("pageSize") String pageSize)
    {
        if(StringUtils.isBlank(params.getYears())){
            throw new RuntimeException("请选择年份");
        }

        Set<String> sysRoles = SecurityUtils.getLoginUser().getRoles();
        SysUser sysUser = SecurityUtils.getLoginUser().getSysUser();

        if(sysUser.isAdmin()){
            sysRoles.add("p_01_011");
        }


        if(ArrayUtil.contains(sysRoles.toArray(),"x_01_011")){
            if(StringUtils.isBlank(params.getCity())){
                params.setCounty(sysUser.getDistrict());
            }else{
                params.setCounty(params.getCity());
                params.setCity(null);
            }
            params.setCity(null);
        }else if(ArrayUtil.contains(sysRoles.toArray(),"s_01_011")){
            if(StringUtils.isBlank(params.getCity())){
                params.setCity(sysUser.getCity());
            }else{
                if(StringUtils.endsWith(params.getCity(),"0000")){
                    params.setProvince(params.getCity());
                    params.setCity(null);
                }else if(StringUtils.endsWith(params.getCity(),"00")){
                    params.setCity(params.getCity());
                }else{
                    params.setCounty(params.getCity());
                    params.setCity(null);
                }
            }
        }else if(ArrayUtil.contains(sysRoles.toArray(),"p_01_011")){
            if(StringUtils.isBlank(params.getCity())){
                params.setProvince(sysUser.getProvince());
            }else{
                if(StringUtils.endsWith(params.getCity(),"0000")){
                    params.setProvince(params.getCity());
                    params.setCity(null);
                }else if(StringUtils.endsWith(params.getCity(),"00")){
                    params.setCity(params.getCity());
                }else{
                    params.setCounty(params.getCity());
                    params.setCity(null);
                }
            }
        }
        params.setDpetCode("shuiliju");
        List<PreMuImportDetailInfo> list = preMuImportDetailInfoService.selectPreMuImportDetailInfoList(params);

        PageResult<PreMuImportDetailInfo> preMuImportDetailInfoPageResult = PageUtils.paginateList(list, Integer.valueOf(pageNum), Integer.valueOf(pageSize));

        return preMuImportDetailInfoPageResult;
    }

    /**
     * 查询自然环境局导入数据详情列表
     */
    @GetMapping("/ziran/list")
    public PageResult<PreMuImportDetailInfo> ziRanList(PreMuImportDetailInfo params, @RequestParam("pageNum") String pageNum,@RequestParam("pageSize") String pageSize)
    {

        if(StringUtils.isBlank(params.getYears())){
            throw new RuntimeException("请选择年份");
        }

        Set<String> sysRoles = SecurityUtils.getLoginUser().getRoles();
        SysUser sysUser = SecurityUtils.getLoginUser().getSysUser();

        if(sysUser.isAdmin()){
            sysRoles.add("p_01_011");
        }

        if(ArrayUtil.contains(sysRoles.toArray(),"x_01_011")){
            if(StringUtils.isBlank(params.getCity())){
                params.setCounty(sysUser.getDistrict());
            }else{
                params.setCounty(params.getCity());
                params.setCity(null);
            }
            params.setCity(null);
        }else if(ArrayUtil.contains(sysRoles.toArray(),"s_01_011")){
            if(StringUtils.isBlank(params.getCity())){
                params.setCity(sysUser.getCity());
            }else{
                if(StringUtils.endsWith(params.getCity(),"0000")){
                    params.setProvince(params.getCity());
                    params.setCity(null);
                }else if(StringUtils.endsWith(params.getCity(),"00")){
                    params.setCity(params.getCity());
                }else{
                    params.setCounty(params.getCity());
                    params.setCity(null);
                }
            }
        }else if(ArrayUtil.contains(sysRoles.toArray(),"p_01_011")){
            if(StringUtils.isBlank(params.getCity())){
                params.setProvince(sysUser.getProvince());
            }else{
                if(StringUtils.endsWith(params.getCity(),"0000")){
                    params.setProvince(params.getCity());
                    params.setCity(null);
                }else if(StringUtils.endsWith(params.getCity(),"00")){
                    params.setCity(params.getCity());
                }else{
                    params.setCounty(params.getCity());
                    params.setCity(null);
                }
            }
        }

//        if(sysUser.isAdmin()){
//            if(StringUtils.isBlank(params.getCity())){
//                params.setProvince("370000");
//            }else{
//                if(StringUtils.endsWith(params.getCity(),"0000")){
//                    params.setProvince(params.getCity());
//                    params.setCity(null);
//                }else if(StringUtils.endsWith(params.getCity(),"00")){
//                    params.setCity(params.getCity());
//                }else{
//                    params.setCounty(params.getCity());
//                    params.setCity(null);
//                }
//            }
//        }else {
//            if(ArrayUtil.contains(sysRoles.toArray(),"x_01_011")){
//                if(StringUtils.isBlank(params.getCity())){
//                    params.setCounty(sysUser.getDistrict());
//                }else{
//                    params.setCounty(params.getCity());
//                    params.setCity(null);
//                }
//                params.setCity(null);
//            }else if(ArrayUtil.contains(sysRoles.toArray(),"s_01_011")){
//                if(StringUtils.isBlank(params.getCity())){
//                    params.setCity(sysUser.getCity());
//                }else{
//                    if(StringUtils.endsWith(params.getCity(),"0000")){
//                        params.setProvince(params.getCity());
//                        params.setCity(null);
//                    }else if(StringUtils.endsWith(params.getCity(),"00")){
//                        params.setCity(params.getCity());
//                    }else{
//                        params.setCounty(params.getCity());
//                        params.setCity(null);
//                    }
//                }
//            }else if(ArrayUtil.contains(sysRoles.toArray(),"p_01_011")){
//                if(StringUtils.isBlank(params.getCity())){
//                    params.setProvince(sysUser.getProvince());
//                }else{
//                    if(StringUtils.endsWith(params.getCity(),"0000")){
//                        params.setProvince(params.getCity());
//                        params.setCity(null);
//                    }else if(StringUtils.endsWith(params.getCity(),"00")){
//                        params.setCity(params.getCity());
//                    }else{
//                        params.setCounty(params.getCity());
//                        params.setCity(null);
//                    }
//                }
//            }
//        }
        params.setDpetCode("guotuju");
        List<PreMuImportDetailInfo> list = preMuImportDetailInfoService.selectPreMuImportDetailInfoList(params);

        PageResult<PreMuImportDetailInfo> preMuImportDetailInfoPageResult = PageUtils.paginateList(list, Integer.valueOf(pageNum), Integer.valueOf(pageSize));

        return preMuImportDetailInfoPageResult;
    }

    /**
     * 导出导入数据详情列表
     */
    @RequiresPermissions("system:info:export")
    @Log(title = "导入数据详情", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, PreMuImportDetailInfo preMuImportDetailInfo)
    {
        List<PreMuImportDetailInfo> list = preMuImportDetailInfoService.selectPreMuImportDetailInfoList(preMuImportDetailInfo);
        ExcelUtil<PreMuImportDetailInfo> util = new ExcelUtil<PreMuImportDetailInfo>(PreMuImportDetailInfo.class);
        util.exportExcel(response, list, "导入数据详情数据");
    }

    /**
     * 获取导入数据详情详细信息
     */
    @RequiresPermissions("system:info:query")
    @GetMapping(value = "/{baseId}")
    public AjaxResult getInfo(@PathVariable("baseId") String baseId)
    {
        return success(preMuImportDetailInfoService.selectPreMuImportDetailInfoByBaseId(baseId));
    }

    /**
     * 新增导入数据详情
     */
    @RequiresPermissions("system:info:add")
    @Log(title = "导入数据详情", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody PreMuImportDetailInfo preMuImportDetailInfo)
    {
        return toAjax(preMuImportDetailInfoService.insertPreMuImportDetailInfo(preMuImportDetailInfo));
    }

    /**
     * 修改导入数据详情
     */
    @RequiresPermissions("system:info:edit")
    @Log(title = "导入数据详情", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody PreMuImportDetailInfo preMuImportDetailInfo)
    {
        return toAjax(preMuImportDetailInfoService.updatePreMuImportDetailInfo(preMuImportDetailInfo));
    }

    /**
     * 删除导入数据详情
     */
    @RequiresPermissions("system:info:remove")
    @Log(title = "导入数据详情", businessType = BusinessType.DELETE)
    @DeleteMapping("/{baseIds}")
    public AjaxResult remove(@PathVariable String[] baseIds)
    {
        return toAjax(preMuImportDetailInfoService.deletePreMuImportDetailInfoByBaseIds(baseIds));
    }

    @GetMapping("/queryImportStatus")
    public AjaxResult queryImportStatus(PreMuImportDetailInfo params){
        Set<String> sysRoles = SecurityUtils.getLoginUser().getRoles();
        SysUser sysUser = SecurityUtils.getLoginUser().getSysUser();

        if(ArrayUtil.contains(sysRoles.toArray(),"x_01_011")){
            if(StringUtils.isBlank(params.getCity())){
                params.setCounty(sysUser.getDistrict());
            }else{
                params.setCounty(params.getCity());
                params.setCity(null);
            }
            params.setCity(null);
        }else if(ArrayUtil.contains(sysRoles.toArray(),"s_01_011")){
            if(StringUtils.isBlank(params.getCity())){
                params.setCity(sysUser.getCity());
            }else{
                if(StringUtils.endsWith(params.getCity(),"0000")){
                    params.setProvince(params.getCity());
                    params.setCity(null);
                }else if(StringUtils.endsWith(params.getCity(),"00")){
                    params.setCity(params.getCity());
                }else{
                    params.setCounty(params.getCity());
                    params.setCity(null);
                }
            }
        }else if(ArrayUtil.contains(sysRoles.toArray(),"p_01_011")){
            if(StringUtils.isBlank(params.getCity())){
                params.setProvince(sysUser.getProvince());
            }else{
                if(StringUtils.endsWith(params.getCity(),"0000")){
                    params.setProvince(params.getCity());
                    params.setCity(null);
                }else if(StringUtils.endsWith(params.getCity(),"00")){
                    params.setCity(params.getCity());
                }else{
                    params.setCounty(params.getCity());
                    params.setCity(null);
                }
            }
        }
        List<PreMuImportDetailInfo> result = preMuImportDetailInfoService.queryImportStatus(params);
        return AjaxResult.success(result);
    }
}
